//
//  WeekView.swift
//  YOKO
//
//  Created by 鲁浩 on 16/5/6.
//  Copyright © 2016年 15Tec. All rights reserved.
//

import UIKit

class WeekView: UIView {

    let fontsize = CGFloat(8)
    let gridWidth = CGFloat(screenWidth/7)
    let gridHeight = CGFloat(56.0)
    let topPerEventViewHeight: CGFloat = 18.0
    
    //TODO
    var topViewLineNum: Int = 0 {
        didSet {
            setNeedsDisplay()
        }
    }

    
    var events = [[EventInWeekView]]() {
        didSet {
            initEventView()
        }
    }
    var topEvents = [EventInWeekView]() {
        didSet {
            initTopEventView()
        }
    }

    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
    }

    
    override func drawRect(rect: CGRect) {
        
         let gridBeginHeight: CGFloat = topPerEventViewHeight * CGFloat(topViewLineNum)
        
        for i in 1...6 {
            
            let drawPath = UIBezierPath()
            
            drawPath.moveToPoint(CGPoint(x: gridWidth * CGFloat(i), y: gridBeginHeight))
            drawPath.addLineToPoint(CGPoint(x: gridWidth * CGFloat(i), y: gridHeight*24+gridBeginHeight))
            drawPath.lineWidth = 0.1
            drawPath.closePath()
            UIColor.blackColor().set()
            drawPath.stroke()
            drawPath.fill()
        }
        
        for i in 0...23 {
            
            let drawPath = UIBezierPath()
            
            drawPath.moveToPoint(CGPoint(x: 0, y: gridHeight * CGFloat(i) + gridBeginHeight))
            drawPath.addLineToPoint(CGPoint(x: screenWidth, y: gridHeight * CGFloat(i) + gridBeginHeight))
            drawPath.lineWidth = 0.1
            drawPath.closePath()
            UIColor.blackColor().set()
            drawPath.stroke()
            drawPath.fill()
            
            let font = UIFont.systemFontOfSize(fontsize)
            if(i != 0){
                NSString(format: "%02d", i).drawInRect(CGRectMake(0, gridHeight * CGFloat(i)-fontsize/1.5 + gridBeginHeight , CGFloat(30), font.lineHeight), withAttributes: [
                    NSFontAttributeName: font,
                    NSForegroundColorAttributeName: YOKOColor.fontGray])
            }else{
                NSString(format: "%02d", i).drawInRect(CGRectMake(0, gridBeginHeight , CGFloat(30), font.lineHeight), withAttributes: [
                    NSFontAttributeName: font,
                    NSForegroundColorAttributeName: YOKOColor.fontGray])
            }
            
        }
        
    }
    
    func initEventView() {
        let perWidth: CGFloat = gridWidth/5
        let perHeight: CGFloat = gridHeight/4
        
//        let views = [EventViewInWeekView]()
        
        for i in 0...events.count-1 {
            let beforeWidth = gridWidth*CGFloat(i)
            for event in events[i] {
                let eventView = EventViewInWeekView()
                
                let x = beforeWidth+perWidth*CGFloat(event.startX)
                let y = perHeight*CGFloat(event.startY)+topPerEventViewHeight*CGFloat(topViewLineNum)
                let width = gridWidth-perWidth*CGFloat(event.startX)
                let height = perHeight*CGFloat(event.timeHeight)
                eventView.frame = CGRectMake(x, y, width, height)
                
                let drawColor = YOKOColor.TypeColors[event.colorType].colorWithAlphaComponent(0.4)
                eventView.backgroundColor = drawColor
                eventView.eventColor = event.colorType
                eventView.titleLabel.text = event.title
                eventView.layer.cornerRadius = 2.5
                
//                views.append(eventView)
                
                self.addSubview(eventView)
                
            }
            
        }
    }
    
    func initTopEventView() {
        
        for event in topEvents {
            let view = EventViewInTopViewInWeekView()
            
            let x = CGFloat(event.startX)*gridWidth+2.0
            let y = CGFloat(event.startY)*topPerEventViewHeight+2.0
            let width = gridWidth*CGFloat(event.dateWidth)-2.0
            let height = topPerEventViewHeight-4.0
            view.frame = CGRectMake(x, y, width, height)
            
            view.eventColor = event.colorType
            view.titleLabel.text = event.title
//            view.yoko_addCorner(withSize: CGSizeMake(4, 4), radius: 2.0)
            
            self.addSubview(view)
        }
        
    }
}
